package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.download.web.entity.BusArticle;
import com.download.web.entity.BusArticleComment;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.AddArticleCommentModel;
import com.download.web.entity.model.CommentModel;
import com.download.web.entity.model.Page;
import com.download.web.mapper.BusArticleCommentMapper;
import com.download.web.mapper.BusArticleMapper;
import com.download.web.service.IBusArticleCommentService;
import com.download.web.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cmy
 * @create: 2019-02-21 19:43
 **/
@Service
public class BusArticleCommentServiceImpl extends ServiceImpl<BusArticleCommentMapper, BusArticleComment> implements IBusArticleCommentService {

    @Resource
    private BusArticleCommentMapper articleCommentMapper;
    @Autowired
    private ISysUserService userService;

    @Resource
    private BusArticleMapper aticleMapper;

    @Override
    public Page<CommentModel> commentListPage(Integer artId, Integer pageIndex, Integer pageSize,Integer objTYpe) {
        List<CommentModel> commentModelList=articleCommentMapper.commentList(artId,pageIndex,pageSize,objTYpe);
        commentModelList.forEach(commentModel -> {
            List<CommentModel> commentReplys=articleCommentMapper.commentReplyList(commentModel.getCommentId(),0,3);
            commentModel.setCommentReply(commentReplys);
            commentModel.setCommentReplySize(articleCommentMapper.commentReplyCount(commentModel.getCommentId()));
        });
        return new Page<>(commentModelList,articleCommentMapper.articleCcommentCount(artId,objTYpe),pageIndex,pageSize);
    }

    @Override
    public String addComment(AddArticleCommentModel articleComment) {
        SysUser user=userService.getOne(new QueryWrapper<SysUser>().eq("email",articleComment.getEmail()));
        if(null==user){
            return "没有此用户，请注册";
        }
        BusArticleComment comment=new BusArticleComment(articleComment.getContent(),user.getId(),0,articleComment.getArticleId(),articleComment.getObjType(),false);
        save(comment);
        return "评论成功";
    }

    @Override
    public Map<String, Object> selectAllList(Integer pageIndex, Integer pageSize,Integer states) {
        try {
            Map<String,Object> map=new HashMap<>();
            String sql="";
            if(states==1){
                sql=" where states=true ";
            }else if(states==2){
                sql=" where states=false ";
            }
            List<Map<String,Object>> list=articleCommentMapper.selectAllPage(sql,(pageIndex-1)*pageSize, pageSize);
            int total=0;
            if(states==0){
                total=articleCommentMapper.selectCount(new QueryWrapper<>());
            }else if(states==1){
                total=articleCommentMapper.selectCount(new QueryWrapper<BusArticleComment>().eq("states",true));
            }else if(states==2){
                total=articleCommentMapper.selectCount(new QueryWrapper<BusArticleComment>().eq("states",false));
            }
            for (Map<String,Object> li:list){
                int userId=Integer.parseInt(li.get("user_id").toString());
                int articleId=Integer.parseInt(li.get("article_id").toString());
                SysUser user=userService.getById(userId);
                if(user!=null&&user.getEmail()!=null) {
                    li.put("user_name", user.getEmail());
                }else{
                    li.put("user_name", "");
                }
                BusArticle article=aticleMapper.selectById(articleId);
                if(article==null){
                    li.put("article_name","文章被删除");
                }else{
                    li.put("article_name",article.getTitle());
                }

            }
            map.put("data",list);
            map.put("totalCount",total);
            map.put("pageIndex",pageIndex);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
